<template>
	<view>
		<!-- 自定义事件 点击 -->
		<my-search @click="gotoSearch"></my-search>
		<!-- scroll-view -->
		<view class="scroll-view-container">
			<!-- 左边 -->
			<scroll-view class="scroll-view-left" scroll-y="true" :style="{height:wh+'px'}">
				<block v-for="(item,index) in categorieList" :key="index">
					<view @click="activeChange(index)" :class="[index === active ?'active' : '']">{{ item.cat_name}}
					</view>
				</block>
			</scroll-view>
			<!-- 右边 -->
			<scroll-view class="scroll-view-right" scroll-y="true" :style="{height:wh+'px'}" :scroll-top="scrollTop">
				<view v-for="(item,index) in cateList2" :key="index">
					<!-- 二级分类的标题 -->
					<view class="cate2-title">
						/ {{item.cat_name}} /
					</view>
					<!-- 三级分类列表 -->
					<view class="cate3-list">
						<!-- 三级每一项 -->
						<view class="cate3-item" v-for="(item3,index3) in item.children" :key="index3"
						@click="gotoGoodsList(item3)">
							<!-- 图片路径出现问题 -->
							<image :src='defaultPath'></image>
							<text>{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMixin from '@/mixins/tabber-badge.js'
	export default {
		mixins:[badgeMixin],
		data() {
			return {
				wh: '',
				categorieList: [],
				// 激活
				active: 0,
				cateList2: [],
				scrollTop: 0,
				defaultPath:'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
			};
		},
		onLoad() {
			// 获取手机信息 我要得到手机的高度
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight-50
			this.getCategorieData()
		},
		methods: {
			async getCategorieData() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/categories')
				// 判断状态码
				if (res.meta.status !== 200) return uni.$showMsg()
				// 获取二级列表
				this.cateList2 = res.message[0].children
				console.log(res.message[0].children);
				this.categorieList = res.message
			},
			activeChange(i) {
				this.active = i
				// 点击了哪个就把谁赋给谁
				this.cateList2 = this.categorieList[i].children
				// 重置滚动条
				this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},
			gotoGoodsList(item3){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?cid='+item3.cat_id
				})
			},
			gotoSearch(){
				uni.navigateTo({
					url:'/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.searchPosition {
		position: sticky;
		top:0;
		z-index: 999;
	}
	.scroll-view-container {
		display: flex;
	}

	.scroll-view-left {
		width: 120px;
		view {
			line-height: 60px;
			text-align: center;
			background-color: #F4F4F4;
			font-size: 12px;

			&.active {
				position: relative;
				background-color: #fff;

				&::before {
					content: '';
					display: block;
					width: 3px;
					height: 30px;
					background-color: #C00000;
					position: absolute;
					top: 50%;
					left: 0;
					transform: translateY(-50%);
				}
			}
		}
	}

	.cate2-title {
		font-size: 12px;
		font-weight: bold;
		text-align: center;
		padding: 15px 0;
	}

	.cate3-list {
		display: flex;
		flex-wrap: wrap;

		image {
			width: 60px;
			height: 60px;
		}
	}

	.cate3-item {
		display: flex;
		width: 33.3%;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-bottom: 10px;

		text {
			font-size: 12px;
		}
	}
</style>